//
//  CYAssetDetailVC.m
//  CreditAddressBook
//
//  Created by Mosu&Cuu on 16/2/22.
//  Copyright © 2016年 Lee. All rights reserved.
//

#import "CYAssetDetailVC.h"
#import "CYUser.h"
#import "CYAssetDetailCell.h"
#import "CYURL.h"
#import "CYHttpTool.h"
#import "CYLoginHttpTool.h"
#import "CYJsonInfo.h"
#import "MJExtension.h"
#import "CYMessageTVCell.h"
#import "CYMessageModel.h"
#import "CYCreditTool.h"
#import "CYTool.h"
#import "CYMessageDtVC.h"
#import "TOWebViewController.h"
#import "LEBarButtonItem.h"
#import "FDSlideBar.h"
#import "CYCommon.h"
#import "LEDIYFooter.h"
#import "MBProgressHUD+MJ.h"
#import "LEDIYHeader.h"
#import "MJRefresh.h"



@interface CYAssetDetailVC ()<UITableViewDataSource,UITableViewDelegate>
{
    UITableView *_messageTabelView ;//下面的tabbleview
    NSMutableArray *_dataArray;//数据源
    
}
@property(nonatomic,strong)UILabel *noticelb;
@property(nonatomic, assign) NSInteger page;
@property(nonatomic, strong) NSString *accountstr;
@property(nonatomic, assign) NSInteger currentPage;

@end

@implementation CYAssetDetailVC

-(void)viewWillAppear:(BOOL)animated{
    
    [self setupNavigationView];
//    [self downloaddata];
     // 马上进入刷新状态
     [_messageTabelView.mj_header beginRefreshing];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"资金明细";
    self.view.backgroundColor = [UIColor whiteColor];
    self.automaticallyAdjustsScrollViewInsets = NO;
//    [_messageTabelView reloadData];
    _dataArray = [[NSMutableArray array]init];
    [self cretetb];
//    [self setupRefresh];
    //    LEBarButtonItem *itema = [[LEBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"icon_overflowmenu"] andSelImage:nil andTitle:@"个人消息" target:self action:@selector(moreBtn)];
    //
    //    self.navigationItem.rightBarButtonItem = itema;
    // Do any additional setup after loading the view.
}
-(void)endRefresh{
     [_messageTabelView.mj_header endRefreshing];
     [_messageTabelView.mj_footer endRefreshing];
}

//- (void)loadData:(BOOL)isPull {
//    if (isPull) {
//        self.page = 0;
//    }
//    CYUser *user = [CYLoginHttpTool userData].user;
//    [CYHttpTool postWithURL:[CYURL shared].getAccountDetails params:@{@"userId":user.userId,@"token":user.token,@"page":@(self.page)} success:^(id json) {
//        CYJsonInfo *jsonInfo = [CYJsonInfo objectWithKeyValues:json];
//        if (jsonInfo.isSuccess) {
//            NSDictionary *data = jsonInfo.data;
//            NSArray *arr = [data valueForKey:@"pageItems"];
//            if (arr && arr.count) {
//                NSArray *temps = [CYAssetDetailmodel objectArrayWithKeyValuesArray:arr];
//                [_dataArray addObjectsFromArray:temps];
//                [MBProgressHUD hideHUDForView:self.view];
//                if (isPull) {
//                    [_dataArray removeAllObjects];
//                    [_dataArray addObjectsFromArray:arr];
//                    [_messageTabelView.mj_header endRefreshing];
//                } else {
//                    [_dataArray addObjectsFromArray:arr];
//                    [_messageTabelView.mj_footer endRefreshing];
//                }
//                [_messageTabelView reloadData];
//                self.page++;
//            } else {
//                [_messageTabelView.mj_footer endRefreshingWithNoMoreData];
//            }
//        }
//        
//    } failure:^(NSError *error) {
//        
//    }];
//    
//}


//- (void)setupRefresh {
//    CYWeakObject(weakself, self);
//    LEDIYFooter *footer = [LEDIYFooter footerWithRefreshingBlock:^{
//        [weakself loadData:NO];
//    }];
//    footer.refreshingTitleHidden = YES;
//    _messageTabelView.mj_footer = footer;
//    
//    LEDIYHeader *header = [LEDIYHeader headerWithRefreshingBlock:^{
//        [weakself loadData:YES];
//    }];
//    //    header.refresh
//    _messageTabelView.mj_header = header;
//    [header beginRefreshing];
//}

- (void)setupNavigationView {
    
    UIView *naviView = [[UIView alloc] init];
    naviView.backgroundColor = CYNaviThemeColor;
    naviView.frame = CGRectMake(0, 0, self.view.width, 64);
    [self.view addSubview:naviView];
     // 添加下拉刷新和上拉加载更多
     __weak typeof(self) weakSelf = self;
     _messageTabelView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
          weakSelf.currentPage = 1;
          [weakSelf downloaddata];
     }];
     
     _messageTabelView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
          weakSelf.currentPage++;
          [weakSelf downloaddata];
     }];
}


-(void)downloaddata{
    CYUser *user = [CYLoginHttpTool userData].user;
     //getAccountDetails   getAccountDetailsMemo
     
    [CYHttpTool postWithURL:[CYURL shared].getAccountDetailsMemo params:@{@"userId":user.userId,@"token":user.token,@"page":@"0"} success:^(id json) {
      //  CYLog(@"message = %@",json);
         [self endRefresh];
        CYJsonInfo *jsonInfo = [CYJsonInfo objectWithKeyValues:json];
        if (jsonInfo.isSuccess) {
            NSDictionary *data = jsonInfo.data;
            NSArray *arr = [data valueForKey:@"pageItems"];
             for (NSDictionary *dict in arr) {
                  CYAssetDetailmodel *model = [[CYAssetDetailmodel alloc]init];
                  [model setValuesForKeysWithDictionary:dict];
                       [_dataArray addObject:model];
             }
             
             [_messageTabelView reloadData];
             
             
//            CYLog(@"arr = %@",data);
//            if (arr) {
//                _noticelb.hidden = YES;
//                NSArray *temps = [CYAssetDetailmodel objectArrayWithKeyValuesArray:arr];
//                    [_dataArray addObjectsFromArray:temps];
//            }
            if (!_dataArray.count) {
                    UIView *cover = [[UIView alloc] initWithFrame:self.view.frame];
                    [self.view addSubview:cover];
                    //添加约束
                    [cover autoPinEdgesToSuperviewEdgesWithInsets:UIEdgeInsetsZero excludingEdge:ALEdgeTop];
                    [cover autoPinEdgeToSuperviewEdge:ALEdgeTop withInset:64];
                    
                    cover.backgroundColor = [UIColor whiteColor];
                    UILabel *label = [[UILabel alloc] initForAutoLayout];
                    [cover addSubview:label];
                    [label autoCenterInSuperview];
                    label.text = @"没有更多数据";
            }
        }
         
    } failure:^(NSError *error) {
        CYLog(@"error = %@",error);
         [self endRefresh];
    }];
}


-(void)cretetb{
    //[[ UIScreen mainScreen ] bounds].size.width
    _messageTabelView = [[UITableView alloc]initWithFrame:CGRectMake(0,64, 100, [[UIScreen mainScreen]bounds].size.height-74) style:UITableViewStylePlain];
    _messageTabelView.backgroundColor = [UIColor whiteColor];
    _messageTabelView.dataSource = self;
    _messageTabelView.delegate = self;
    [_messageTabelView registerNib:[UINib nibWithNibName:@"CYAssetDetailCell" bundle:nil] forCellReuseIdentifier:@"CYAssetDetailCell"];
    [CYTool setExtraCellLineHiddenWithTableView:_messageTabelView];
    [self.view addSubview:_messageTabelView];
    [_messageTabelView autoPinEdgesToSuperviewEdgesWithInsets:UIEdgeInsetsZero excludingEdge:ALEdgeTop];
    [_messageTabelView autoPinEdgeToSuperviewEdge:ALEdgeTop withInset:64];
}

#pragma mark - tableview代理
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    CYAssetDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CYAssetDetailCell" forIndexPath:indexPath];
    //cell.model = _dataArray[indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
   cell.model = _dataArray[indexPath.row];
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return  70;
    
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
      return _dataArray.count;

}
#pragma mark -
-(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self= [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
    }
    return self;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
